import {
    request
} from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cateTit: [], //左侧导航
        goodsList: [], //商品列表
        currentIndex: 0, //被点击的索引值
        scrollTop: 0,
    },
    cates: [],

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCate();
        //获取本地存储中的数据（小程序中也是存在本地存储技术）
        const Cates = wx.getStorageSync('cates');
        //判断
        if (!Cates) {
            //不存在 发送请求获取数据
            this.getCate();
        }
    },

    async getCate() {
        // request({
        //     url: "/categories"
        // }).then(res => {
        //     // console.log(res.data.message.cat);
        //     this.cates = res.data.message;
        //     //把接口的数据存入到本地存储中
        //     wx.setStorageSync('cates', {time:Date.now(),data:this.cates})
        //     let cateTit = this.cates.map(v => v.cat_name)
        //     let goodsList = this.cates[0].children;
        //     this.setData({
        //         cateTit,
        //         goodsList
        //     })
        // })
        const res = await request({
            url: "/categories"
        });
        this.Cates = res.data.message;
        //将接口的数据存入到本地存储中
        wx.setStorageSync('Cates', {
            time: Date.now(),
            data: this.Cates
        })
        let cateTit = this.Cates.map(v => v.cat_name)
        let goodsList = this.Cates[0].children;
        this.setData({
            cateTit,
            goodsList
        })
    },
    // 左侧事件的点击事件
    handleItemTap(e) {
        console.log(e);
        const {
            index
        } = e.currentTarget.dataset;
        let goodsList = this.Cates[index].children;
        this.setData({
            currentIndex: index,
            goodsList,
            scrollTop: 0,
        })
    }

})